<?php
class Counter{

	public function runCounter(){
        $actionName=$this->action->id;
        $controllerName=$this->uniqueid;
        $sum=0;
        $counter=array();
        $counter=Yii::app()->session['counter'];

        if($counter!=null){
            if($actionName=='create'){//&&$counter['flag']){
                //$counter['flag']=false;//true,false
                //throw new Exception("Error Processing Request -1".$actionName);
                $counter['flag']=$counter['flag']!=true? true:false;
                
            }if($actionName!='çreate'){//&&$counter['flag']){
                //$counter['flag']=false;
                //throw new Exception("Error Processing Request -1".$actionName);
                $counter['flag']=$counter['flag']!=true? false:true;
                //throw new Exception("Error Processing Request".$counter['flag']);
            }else{
                $counter['flag']=$counter['flag']!=true? true:false;
                //$counter['flag']=true;
            }

        
        	if($actionName=='create' && !$counter['flag']){
                /*
                CounterBralink::model()->updateCounters(array( 'sum' => 1),
                                  'type="'.$controllerName.'"'
                          );
                
                $sum=CounterBralink::model()->find('type=:type',array(':type'=>$controllerName))->sum;*/
                $counter['flag']=false;
        	}
            else if($actionName!='create' && !$counter['flag']){
                CounterBralink::model()->updateCounters(array( 'sum' => -1),
                                  'type="'.$controllerName.'"'
                          );
                
                $counter['flag']=true;
            }
        }
        Yii::app()->session['counter']=array('flag'=>$counter['flag'],'userLogin'=>$sum);

    	//return parent::beforeAction($action);
    }

    public function editCounter(){
    	$actionName=$this->action->id;
        $controllerName=$this->uniqueid;
        $sum=0;
        $counter=array();
        $counter=Yii::app()->session['counter'];

        CounterBralink::model()->updateCounters(array( 'sum' => 1),
                          'type="'.$controllerName.'"'
                  );
        
        $sum=CounterBralink::model()->find('type=:type',array(':type'=>$controllerName))->sum;

        Yii::app()->session['counter']=array('flag'=>$counter['flag'],'userLogin'=>$sum);
    }
	
}
?>